<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/5/30
 * Time: 11:10
 */

namespace app\admin\controller;


use app\admin\model\ClassType;
use app\admin\model\DriverType;
use app\admin\model\OrderList;
use PHPExcel_IOFactory;
use PHPExcel;
class Order extends Base
{



    public function orderList(){
        return $this->fetch();
    }

    public function orderData($page,$limit){
        $where = input('get.where')?[['true_name','like','%'.input('get.where').'%']]:"";
        $type = input('get.test_type')?['test_type'=>(int)input('get.test_type')]:"";
        $data = OrderList::where($where)
            ->where($type)
            ->withAttr('order_type',function ($value){
                $data = [1=>'单买订单',2=>'拼团订单'];
                return $data[$value];
            })->withAttr('order_status',function ($value){
                $data = [0=>'未付款',1=>'已付款'];
                return $data[$value];
            })->withAttr('test_type',function ($value){
                $data = [1=>'在线报名',2=>'补考缴费',3=>'预约练车'];
                return $data[$value];
            })->page($page,$limit)->select()->each(function ($item){
            $item->course_type = ClassType::where('id',$item->course_type)->value('class_name');
        })->toArray();
        $count = OrderList::where($where)->where($type)->count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }

    public function orderSee($id){
        $data = OrderList::where('id',$id)
            ->withAttr('order_type',function ($value){
                $data = [1=>'单买订单',2=>'拼团订单'];
                return $data[$value];
            })->withAttr('order_status',function ($value){
                $data = [0=>'未付款',1=>'已付款'];
                return $data[$value];
            })->withAttr('test_type',function ($value){
                $data = [1=>'在线报名',2=>'补考缴费',3=>'预约练车'];
                return $data[$value];
            })->withAttr('pay_type',function ($value){
                return '微信支付';
            })->withAttr('course_type',function ($value){
                return ClassType::where('id',$value)->value('class_name');
            })->withAttr('sign_type',function ($value){
                return DriverType::where('id',$value)->value('name');
            })->withAttr('coach_id',function ($value){
                return \app\admin\model\Coach::where('id',$value)->value('name');
            })->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function orderExport(){
        $title = [
            ['good_name','商品名称'],['true_name','姓名'],['phone','电话'],['id_card','身份证号码']
            ,['order_no','订单号'],['pay_no','交易单号'],['pay_money','支付金额'],['discount_money','优惠金额']
            ,['pay_type','支付方式'],['pay_time','支付时间'],['test_type','报考类型'],['sign_type','驾照类型']
            ,['course_type','科目类型'],['order_type','订单类型'],['order_status','订单状态'],['create_time','下单时间']];
        $start = input('post.start_time')?[['create_time','>',strtotime(input('post.start_time'))]]:"";
        $end  = input('post.end_time')?[['create_time','<',strtotime(input('post.end_time'))]]:"";
        $status = input('post.status')?['create_time'=>input('post.status')]:"";
        $data = OrderList::where($status)->where($start)->where($end)
            ->field('good_name,true_name,phone,id_card,order_no,pay_no,
            pay_money,discount_money,pay_type,pay_time,test_type,
            sign_type,course_type,order_type,order_status,create_time')
            ->withAttr('order_type',function ($value){
                $data = [1=>'单买订单',2=>'拼团订单'];
                return $data[$value];
            })->withAttr('order_status',function ($value){
                $data = [0=>'未付款',1=>'已付款'];
                return $data[$value];
            })->withAttr('test_type',function ($value){
                $data = [1=>'在线报名',2=>'补考缴费',3=>'预约练车'];
                return $data[$value];
            })->withAttr('pay_type',function ($value){
                return '微信支付';
            })->select()->each(function ($item){
                $item->course_type = ClassType::where('id',$item->course_type)->value('class_name');
                $item->sign_type = DriverType::where('id',$item->sign_type)->value('name');
            })->toArray();
        $name = '延长驾校订单';
        $this->exportExcel($name,$title,$data);
    }


    protected function exportExcel($expTitle,$expCellName,$expTableData){
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $expTitle.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);

        $objPHPExcel = new PHPExcel();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

        for($i=0;$i<$cellNum;$i++){//有几列
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'1', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum;$i++){//有几行
            for($j=0;$j<$cellNum;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+2), $expTableData[$i][$expCellName[$j][0]]);
            }
        }
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }


}